package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.fragments.LoginFragment;
import com.sxm.yiwei.fragments.RegistFragment;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import cn.jpush.android.api.JPushInterface;

public class UserLoginActivity extends BaseActivity implements View.OnClickListener{
    LoginFragment loginFragment;
    RegistFragment registFragment;
    private RelativeLayout regist;
    private RelativeLayout login;
    private ImageView loginsan;
    private ImageView registsan;
    private ImageView userloginback;
    private TelephonyManager telephonyManager;
    private String appid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_login);
        SharedPreferences sp = getSharedPreferences("userlogin", Context.MODE_PRIVATE);

        String longitude = sp.getString("longitude", "");
        String latitude = sp.getString("latitude", "");
        String appId = sp.getString("appid", "");
        initLoginView();
        SelectFragment(0);
        String registrationID = JPushInterface.getRegistrationID(this);
        Log.d("tag", "onCreate: 你好---"+registrationID);
        fixingregst(this,latitude,longitude,registrationID);

    }

        // 防止fragment重叠

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //super.onSaveInstanceState(outState);
    }

    //显示
    private void SelectFragment(int i) {
        FragmentTransaction loginft = getFragmentManager().beginTransaction();
        hideFragment(loginft);
        switch (i){
            case 0:
                if (loginFragment==null){
                    loginFragment = new LoginFragment();
                    loginft.add(R.id.login_fragment,loginFragment);

                }else {
                    loginft.show(loginFragment);

                }
                break;
            case 1:
                if (registFragment==null){
                    registFragment = new RegistFragment();
                    loginft.add(R.id.login_fragment,registFragment);
                }else {
                    loginft.show(registFragment);
                }
                break;
        }


        loginft.commit();

    }
        // 隐藏
    private void hideFragment(FragmentTransaction loginft) {
        if (loginFragment!=null){
            loginft.hide(loginFragment);
        }
        if (registFragment!=null){
            loginft.hide(registFragment);
        }
    }

    // 初始化
    private void initLoginView() {
        regist = (RelativeLayout) findViewById(R.id.login_regist);
        login = (RelativeLayout) findViewById(R.id.login_login);
        loginsan = (ImageView) findViewById(R.id.login_san);
        registsan = (ImageView) findViewById(R.id.regist_san);
        userloginback = (ImageView) findViewById(R.id.userlogin_backimg);

        userloginback.setOnClickListener(this);
        login.setOnClickListener(this);
        regist.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.userlogin_backimg:
                finish();
                break;
            case R.id.login_login:
                SelectFragment(0);
                loginsan.setVisibility(View.VISIBLE);
                registsan.setVisibility(View.GONE);
                break;
            case R.id.login_regist:
                SelectFragment(1);
                loginsan.setVisibility(View.GONE);
                registsan.setVisibility(View.VISIBLE);
                break;
        }
    }


    // 设备注册和验证
    public void fixingregst(Context context, String latitude, String longitude, String rid) {
        final String registrationID = JPushInterface.getRegistrationID(this);
        final String s = JPushInterface.getRegistrationID(context);
        telephonyManager = (TelephonyManager) UserLoginActivity.this.getSystemService(Context.TELEPHONY_SERVICE);
        String line1Number = Build.VERSION.RELEASE;
        String pin =  Build.MODEL;
        String Imsi =  telephonyManager.getDeviceId();
        RequestParams params = new RequestParams(InternetAddressUtis.Url+"/equipment/register");
        params.setHeader("Content-Type","application/json");
        com.alibaba.fastjson.JSONObject object = new com.alibaba.fastjson.JSONObject();
        object.put("deviceId",Imsi);
        object.put("brand",pin);
        object.put("version",line1Number);
        object.put("registrationId",registrationID);
        object.put("longitude",longitude);
        object.put("latitude",latitude);
        params.setBodyContent(object.toJSONString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.d("tag", "onSuccess: 设备注册--"+result+"//"+registrationID);
                com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
                // 获取appid
                String data = obj.getString("data");
                com.alibaba.fastjson.JSONObject obj1 = JSON.parseObject(data);
                appid = obj1.getString("appId");
                SharedPreferences sharedPreferences = getSharedPreferences("userlogin",Context.MODE_PRIVATE);
                SharedPreferences.Editor edit = sharedPreferences.edit();
                edit.putString("rid", s);
                edit.putString("appid", appid);
                edit.commit();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d("tag", "onError: 设备注册--"+ex);

            }
            @Override
            public void onCancelled(CancelledException cex) {

            }
            @Override
            public void onFinished() {
            }
        });
    }
}
